package cn.iocoder.yudao.module.shop.info.controller.app;

import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.business.audit.enums.AuditStatusEnum;
import cn.iocoder.yudao.framework.business.eligibility.enums.EligibilityCheckStepEnum;
import cn.iocoder.yudao.framework.business.order.pojo.vo.OrderSubmitVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import cn.iocoder.yudao.module.api.shop.info.enums.ShopStatusEnum;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopEnterOrderDO;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopEnterPackageDO;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopInfoDO;
import cn.iocoder.yudao.module.shop.info.service.ShopEnterOrderService;
import cn.iocoder.yudao.module.shop.info.service.ShopEnterPackageService;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import cn.iocoder.yudao.module.shop.info.vo.AppMyShopInfoVO;
import cn.iocoder.yudao.module.shop.info.vo.AppShopEnterOrderCheckRespVO;
import cn.iocoder.yudao.module.shop.info.vo.AppShopEnterOrderCreateReqVO;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterOrderRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType;

@Tag(name = "用户 App - 店铺缴费")
@RestController
@RequestMapping("/shop/enter-order")
@Validated
@Slf4j
public class AppShopEnterOrderController {

    @Resource
    private ShopEnterOrderService shopEnterOrderService;
    @Resource
    private ShopEnterPackageService shopEnterPackageService;
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private ShopCategoryService shopCategoryService;

    @GetMapping("/check")
    @Operation(summary = "检查我的入驻缴费情况")
    public CommonResult<AppShopEnterOrderCheckRespVO> checkMyShop() {
        AppShopEnterOrderCheckRespVO respVO = new AppShopEnterOrderCheckRespVO();
        ShopInfoDO shopInfoDO = shopInfoService.getDetailByUserId(getLoginUserId(), getLoginUserType());
        if (shopInfoDO == null) {
            respVO.setStep(EligibilityCheckStepEnum.NO_DATA.getValue());
            respVO.setInfo(new AppMyShopInfoVO());
            return success(respVO);
        }
        AppMyShopInfoVO info = BeanUtils.toBean(shopInfoDO, AppMyShopInfoVO.class);
        RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, info.getCountyCode(), info);
        info.assignCategory(shopCategoryService.getCategory(shopInfoDO.getCategoryId()));
        ShopEnterPackageDO enterPackageDO = shopEnterPackageService.getEnterPackage(info.getEnterPackageId());
        if (enterPackageDO != null) {
            info.setEnterPackageName("%s元/%s年".formatted(MoneyUtils.fenToYuanStr(NumberUtil.nullToZero(enterPackageDO.getPrice())), enterPackageDO.getYear()));
        }
        respVO.setInfo(info);
        if (Objects.equals(shopInfoDO.getAuditStatus(), AuditStatusEnum.WAIT.getValue())) {
            respVO.setStep(EligibilityCheckStepEnum.AUDIT_WAIT.getValue());
            return success(respVO);
        }
        if (Objects.equals(shopInfoDO.getAuditStatus(), AuditStatusEnum.REJECT.getValue())) {
            respVO.setStep(EligibilityCheckStepEnum.AUDIT_REJECT.getValue());
            return success(respVO);
        }
        if (Objects.equals(shopInfoDO.getStatus(), ShopStatusEnum.OPENING.getValue())
            || Objects.equals(shopInfoDO.getStatus(), ShopStatusEnum.CLOSING.getValue())) {
            respVO.setStep(EligibilityCheckStepEnum.SUCCESS.getValue());
            return success(respVO);
        }
        ShopEnterOrderDO enterOrderDO = shopEnterOrderService.getEnterOrder(getLoginUserLastShopId());
        if (enterOrderDO == null) {
            respVO.setStep(EligibilityCheckStepEnum.NEED_BUY.getValue());
            respVO.setOrder(new ShopEnterOrderRespVO());
            return success(respVO);
        }
        respVO.setOrder(BeanUtils.toBean(enterOrderDO, ShopEnterOrderRespVO.class));
        if (enterOrderDO.getPayed()) {
            respVO.setStep(EligibilityCheckStepEnum.NO_DATA.getValue());
        } else {
            respVO.setStep(EligibilityCheckStepEnum.NEED_PAY.getValue());
        }
        return success(respVO);
    }

    @PostMapping("/submit")
    @Operation(summary = "提交店铺入驻缴费单")
    public CommonResult<OrderSubmitVO> submitEnterOrder(@Valid @RequestBody AppShopEnterOrderCreateReqVO reqVO) {
        OrderSubmitVO respDTO = shopEnterOrderService.submitEnterOrder(reqVO);
        return success(respDTO);
    }

}
